package com.guldan.thrall.web.controller;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.guldan.core.controller.BaseController;
import com.guldan.core.dubbo.client.ResponseDTO;
import com.guldan.core.response.ResponseJson;
import com.guldan.core.search.Pagination;
import com.guldan.jaina.dto.area.AreaDTO;
import com.guldan.jaina.dto.area.AreaSearchDTO;
import com.guldan.jaina.provider.IAreaProvider;

/**
 * Created by Silas.
 * Date: 2016/11/10
 * Time: 22:06
 */
@Controller
@RequestMapping("/area")
public class AreaController extends BaseController {

	@Value("${xls_download_path}")
	private String xlsDownloadPath;
	
    @Autowired
    private IAreaProvider iAreaProvider;

    //获取单个区域
    @RequestMapping(value = "/get", method = RequestMethod.POST)
    @ResponseJson
    public
    @ResponseBody
    AreaDTO get(@RequestBody AreaDTO dto) {
        ResponseDTO<AreaDTO> response = iAreaProvider.get(dto.getAreaId());
        return response.getData();

    }

    //新增/保存单个区域
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseJson
    public
    @ResponseBody
    AreaDTO save(@RequestBody AreaDTO dto) throws Exception {
        ResponseDTO<AreaDTO> response = iAreaProvider.save(dto);
        if (response.isSuccess())
            return response.getData();
        else {
            throw response.getException();
        }
    }

    //删除单个区域
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseJson
    public
    @ResponseBody
    Boolean delete(@RequestBody AreaDTO dto) {
        ResponseDTO<Boolean> response = iAreaProvider.delete(dto.getAreaId());
        return response.getData();
    }

    //获取区域列表
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseJson
    public
    @ResponseBody
    Pagination<AreaDTO> list(@RequestBody AreaSearchDTO dto) {
        ResponseDTO<Pagination<AreaDTO>> response = iAreaProvider.list(dto);
        return response.getData();
    }

    /**
     * 下载批量上传模板
     * @return
     * @throws IOException
     */
	@RequestMapping(value = "/exportTemplate", method = RequestMethod.GET)
    public ResponseEntity<byte[]> shopExport() throws IOException {
    	return this.downloadExcel(xlsDownloadPath + "areaInfo.xlsx", "区域信息批量导入模板文件");
    }
    
    
}
